package sing.core.ext

import android.content.Context
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import sing.core.R
import sing.core.base.appContext
import sing.core.ext.util.toHtml
import sing.core.weight.ScaleTransitionPagerTitleView

fun MagicIndicator.bindViewPager(viewPager: ViewPager, mStringList: List<String> = arrayListOf(), setSelectFont:Boolean = false, setNoSelectBold:Boolean = false, mTextSize :Float = 16F, action: (index: Int) -> Unit = {}) {
    val commonNavigator = CommonNavigator(appContext)
    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount() = mStringList.size
        override fun getTitleView(context: Context, index: Int): IPagerTitleView {
            return ScaleTransitionPagerTitleView(appContext).apply {
                text = mStringList[index].toHtml() //设置文本
                textSize = mTextSize //字体大小
                normalColor = ContextCompat.getColor(context, R.color.grey) //未选中颜色
                selectedColor = ContextCompat.getColor(context, R.color.main_color) //选中颜色
                setOnClickListener { //点击事件
                    viewPager.currentItem = index
                    action.invoke(index)
                }
            }
        }
        override fun getIndicator(context: Context): IPagerIndicator {
            return LinePagerIndicator(context).apply {
                mode = LinePagerIndicator.MODE_EXACTLY
                lineHeight = UIUtil.dip2px(appContext, 3.0).toFloat()  //线条的宽高度
                lineWidth = UIUtil.dip2px(appContext, 10.0).toFloat()
                roundRadius = UIUtil.dip2px(appContext, 1.5).toFloat() //线条的圆角
                startInterpolator = AccelerateInterpolator()
                endInterpolator = DecelerateInterpolator(2.0f)
                setColors(ContextCompat.getColor(context, R.color.main_color))  //线条的颜色
            }
        }
    }
    this.navigator = commonNavigator

    viewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            this@bindViewPager.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }

        override fun onPageSelected(position: Int) {
            this@bindViewPager.onPageSelected(position)
            action.invoke(position)
        }

        override fun onPageScrollStateChanged(state: Int) {
            this@bindViewPager.onPageScrollStateChanged(state)
        }
    })
}

fun MagicIndicator.bindViewPager2(viewPager: ViewPager2, mStringList: List<String> = arrayListOf(), action: (index: Int) -> Unit = {}) {
    val commonNavigator = CommonNavigator(appContext)
    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount() = mStringList.size
        override fun getTitleView(context: Context, index: Int): IPagerTitleView {
            return ScaleTransitionPagerTitleView(appContext).apply {
                text = mStringList[index].toHtml() //设置文本
                textSize = 16f //字体大小
                normalColor = ContextCompat.getColor(context, R.color.grey) //未选中颜色
                selectedColor = ContextCompat.getColor(context, R.color.main_color) //选中颜色
                setOnClickListener { //点击事件
                    viewPager.currentItem = index
                    action.invoke(index)
                }
            }
        }
        override fun getIndicator(context: Context): IPagerIndicator {
            return LinePagerIndicator(context).apply {
                mode = LinePagerIndicator.MODE_EXACTLY
                lineHeight = UIUtil.dip2px(appContext, 3.0).toFloat()  //线条的宽高度
                lineWidth = UIUtil.dip2px(appContext, 10.0).toFloat()
                roundRadius = UIUtil.dip2px(appContext, 1.5).toFloat() //线条的圆角
                startInterpolator = AccelerateInterpolator()
                endInterpolator = DecelerateInterpolator(2.0f)
                setColors(ContextCompat.getColor(context, R.color.main_color))  //线条的颜色
            }
        }
    }
    this.navigator = commonNavigator

    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            this@bindViewPager2.onPageSelected(position)
            action.invoke(position)
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            this@bindViewPager2.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            this@bindViewPager2.onPageScrollStateChanged(state)
        }
    })
}
